<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>{$web_name}-权限规则</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="__HOME__/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="__HOME__/static/layuiadmin/style/admin.css" media="all">
    <style type="text/css">
        .layui-table-cell{
            text-align: center;
            height: auto;
            white-space: normal;
        }
    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <script type="text/html" id="toolbarDemo">

                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="addRole">添加</button>
                    <button class="layui-btn layui-btn-sm" lay-event="delRole">删除</button>
                </div>
            </script>

            <table id="menu" class="layui-table" lay-filter="menu"></table>
            <script type="text/html" id="table-applet-user">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="introduction"><i class="layui-icon layui-icon-edit"></i>介绍</a>
                <a class="layui-btn  layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-set-sm"></i>详细</a>
            </script>
            <!--这个是选择框tpl-->

        </div>
    </div>
</div>

<script src="__HOME__/static/layuiadmin/layui/layui.js"></script>
<script src="__HOME__/static/layuiadmin/jquery.min.js"></script>
<script>
    layui.config({
        base: '__HOME__/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: '/lib/index', //主入口模块
        treetable : 'treetable-lay/treetable',
    }).use(['index','treeTable', 'layer'], function () {
        var $ = layui.$
            ,treeTable = layui.treeTable;
        //Ajax设置
        $.ajaxSetup({
            type: 'POST',
            dataType : "json",
            data:{type:1},
            cache:false,
            crossDomain: true,
            // 发送cookie
            xhrFields: {
                withCredentials: true
            },
        });
        //渲染表格
        var insTb = treeTable.render({
            loading: true, //翻页加loading
            elem: '#menu',
            toolbar: '#toolbarDemo',
            url:  '{fun:ctr("adminMenuList")}',
            tree: {
                iconIndex: 1,           // 折叠图标显示在第几列
                isPidData: true,        // 是否是id、pid形式数据
                idName: 'id',  // id字段名称
                pidName: 'parent_id'     // pid字段名称
            },
            cols: [[
                {type:'checkbox'},
                {field: 'name', title: '名称'},
                {field: 'icon', title: '图标', templet: function(d){
                        return '<i class="layui-icon '+d.icon+'"></i> ';
                    }
                },
                {field: 'is_menu', title: '是否菜单', templet: function(d){
                        if(d.is_menu==1){
                            return '<a style="color: #2ecc71">是</a>';
                        }else{
                            return '<a style="color: #e51a1a">否</a>';
                        }
                    }
                },
                {field: 'action_name', title: '控制器名称'},
                {field: 'function_name', title: '方法名称'},
                {field: 'sort', title: '排序'}
            ]],
            done: function (res) {
                if (res.code == 403) {
                    location.href = '{fun:ctr("../Iframe/errorHelp")}';
                }else if (res.code == 407) {
                    location.href = '{fun:ctr("../Iframe/error_state")}';
                }else if (res.code == -2) {
                    layer.msg(res.msg, {icon: 2});
                    window.setTimeout(function () {
                        top.location = '{fun:ctr("../Login")}';
                    }, 1000);
                }
            }
        });
        //事件
        treeTable.on('toolbar(menu)', function(obj){
            var checkStatus = insTb.checkStatus();
           /* console.log(checkStatus);*/
            if(obj.event === 'addRole'){
                parent.layer.open({
                    type: 2,
                    title: '添加权限规则',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['65%', '75%'],
                    content: '{fun:ctr("../Iframe/addPermission")}',
                });
            }else if(obj.event === 'delRole'){
              var index = layer.load(5, {time: 30*1000});
                var ids="";
                for(var i=0; i<checkStatus.length; i++){
                    if(checkStatus[i].LAY_INDETERMINATE==false){
                        ids += checkStatus[i].id;
                        if(i !=checkStatus.length-1){
                            ids += ",";
                        }

                    }
                }
                $.ajax({
                    url: '{fun:ctr("delPermissionData")}',
                    data:{role_id:ids},
                    success:function(res){
                        if(res.code == 200){
                            layer.msg(res.msg, {icon: 1});
                            window.setTimeout(function () {
                                window.location.reload();
                            }, 500);
                        }else if(res.code == -7){
                            layer.msg(res.msg, {icon: 2});
                            window.setTimeout(function () {
                                top.location = '{fun:ctr("../Login")}';
                            }, 1000);
                        }else{
                            layer.msg(res.msg, {icon: 2});
                        }
                        layer.close(index);
                    },
                    error: function(res){
                        console.log(res);
                        layer.msg('数据异常，操作失败！');
                    },
                });
            }
        })
    })
</script>
</body>
</html>
